package com.smart.wap.app;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.DesensitizedUtil;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.manager.member.entities.assembler.FriendDto;
import com.smart.manager.member.entities.bodys.RMemberFriend;
import com.smart.manager.member.entities.models.MemberMaterialPig;
import com.smart.manager.member.entities.views.MemberView;
import com.smart.manager.member.entities.views.query.QMemberView;
import com.smart.manager.member.repository.MemberFriendRepository;
import com.smart.utils.MemberAcl;
import com.smart.utils.MemberStp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员资料信息接口
 *
 * @author Administrator
 */
@Api(tags = "WTB-移动端-会员好友")
@RestController
@RequestMapping(SmartConstant.WAP_API_PREFIX + "app/friend")
@RequiredArgsConstructor
@Log4j2
public class FriendController {

    private final MemberFriendRepository friendRepository;


    @ApiOperation(value = "好友列表")
    @GetMapping(value = "friendList")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<List<FriendDto>> getFriendList() {
        Long loginId = MemberAcl.getLoginId();
        List<FriendDto> myFriendList = friendRepository.getMyFriendList(loginId);
        return AjaxResult.data(myFriendList);
    }


    @ApiOperation(value = "随机获取X个陌生人")
    @GetMapping(value = "strangerList/{limit}")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<List<FriendDto>> getStrangerList(@PathVariable Integer limit) {
        Long loginId = MemberAcl.getLoginId();
        List<FriendDto> myFriendList = friendRepository.getStrangerListByRandom(loginId ,limit);
        return AjaxResult.data(myFriendList);
    }







    @ApiOperation(value = "查看申请列表")
    @GetMapping(value = "applyList")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<List<FriendDto>> getApplyList() {
        Long loginId = MemberAcl.getLoginId();
        List<FriendDto> result = friendRepository.getApplyList(loginId);
        return AjaxResult.data(result);
    }

    @ApiOperation(value = "查看好友所有猪仔")
    @GetMapping(value = "getFriendAll/{friendId}")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<List<MemberMaterialPig>> getFriendAll(@PathVariable Long friendId) {
        Long loginId = MemberAcl.getLoginId();
        List<MemberMaterialPig> friendAll = friendRepository.getFriendAll(loginId, friendId);
        return AjaxResult.data(friendAll);
    }


    @ApiOperation(value = "查找好友")
    @PostMapping(value = "searchFriend")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<MemberView> searchFriend(@RequestBody @Validated RMemberFriend.SearchFriendBody body) {
        body.checkSign();
        MemberView memberView = new QMemberView().phoneNumber.eq(body.getPhoneNumber()).setMaxRows(1).findOne();
        Assert.notNull(memberView, "会员信息不存在");
        String tmStr = DesensitizedUtil.mobilePhone(memberView.getPhoneNumber());
        memberView.setPhoneNumber(tmStr);
        return AjaxResult.data(memberView);
    }


    @ApiOperation(value = "申请好友")
    @PostMapping(value = "applyFriend")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> applyFriend(@RequestBody @Validated RMemberFriend.ApplyBody body) {
        friendRepository.applyFriend( MemberAcl.getLoginId(), body);
        return AjaxResult.success("好友申请已发送,等待对方同意");
    }


    @ApiOperation(value = "审核好友")
    @PostMapping(value = "auditFriend")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> auditFriend(@RequestBody @Validated RMemberFriend.AuditBody body) {
        friendRepository.auditFriend( MemberAcl.getLoginId(), body);
        return AjaxResult.success();
    }


    @ApiOperation(value = "删除好友")
    @PostMapping(value = "remove/{token}")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> removeFriend(@PathVariable String token) {
        friendRepository.removeFriend( MemberAcl.getLoginId(), token);
        return AjaxResult.success();
    }






}
